package com.ming.sps.server.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ming.sps.server.bean.ParamMap;
import com.ming.sps.server.services.TerminalService;
import com.ming.sps.server.utils.DateTimeUtil;
import com.ming.sps.server.utils.JsonUtil;
import com.ming.sps.server.utils.MingUtil;

import net.sf.json.JSONObject;

//@Controller
public class TerminalBaseAction extends BaseAction {

    /**
     *
     */
    private static final long serialVersionUID = -1596728434132019639L;

    private TerminalService service = new TerminalService();

    @RequestMapping("init")
    @ResponseBody
    public String init() throws Exception {
        //获取请求的参数
        String terminalName = getRequest("mname", "");
        String token = getRequest("token", "");
        System.out.println(getRequest("json", ""));
        ParamMap terminal = service.getMachine(terminalName);//获取终端信息
        JSONObject jsonObject = new JSONObject();
        String ret = "";
        if (!service.auth(terminalName, token)) {
            ret = retError("未授权，无法访问！", null);
        } else if (terminal.size() == 0) {
            ret = retError("终端信息未配置。", null);
        } else {
            jsonObject = JsonUtil.put(jsonObject, "terminal", terminal);
            jsonObject = JsonUtil.put(jsonObject, "module", service.getModule(terminalName));//获取模块
            jsonObject = JsonUtil.put(jsonObject, "license", service.getLicense(terminalName));//获取许可业务
            jsonObject = JsonUtil.put(jsonObject, "publish", service.getPublish(terminalName));//获取公告信息

            ret = jsonObject.toString();

        }
        return MingUtil.encrypt(ret);
    }

    @RequestMapping("updatePaper")
    @ResponseBody
    public String updatePaper() throws Exception {
        int flag = service.updateMachineByName(requestParam(getRequest()));
        String ret = "";
        if (flag == 0) {
            ret = retError("提交失败。", "submitError");
        } else {
            ret = retSuccess("添加纸张成功！", "addpaper");
        }
        return MingUtil.encrypt(ret);
    }

    @RequestMapping("getTerminal")
    @ResponseBody
    public String getTerminal() throws Exception {
        JSONObject jsonObject = new JSONObject();
        ParamMap terminal = service.getMachine(getRequest("terminal", ""));//获取终端信息
        jsonObject = JsonUtil.put(jsonObject, "terminal", terminal);
        return MingUtil.encrypt(jsonObject.toString());
    }

    @RequestMapping("update")
    @ResponseBody
    public String update() throws Exception {
        Map<String, Object> map = requestParam(getRequest());
        map.put("dtsystemupdatetime", DateTimeUtil.getDateTime());
        System.out.println(map.toString());
        int flag = service.updateMachineByName(map);
        System.out.println(flag);
        JSONObject jsonObject = new JSONObject();
        jsonObject = JsonUtil.put(jsonObject, "terminal", flag);
        return MingUtil.encrypt(jsonObject.toString());
    }

    @RequestMapping("updatePrinttype")
    @ResponseBody
    public String updatePrinttype() throws Exception {
        List param = new ArrayList();
        param.add(getRequest("terminal", ""));
        param.add(getRequest("printtype", ""));
        int flag = dao.updateData("update Z_TM_PRINTTYPE_UPDATE set state = 1 where terminal=? and printtype=?", param);
        JSONObject jsonObject = new JSONObject();
        jsonObject = JsonUtil.put(jsonObject, "updatePrinttype", flag);
        return MingUtil.encrypt(jsonObject.toString());
    }
    
    @RequestMapping("getPublishDetail")
    @ResponseBody
    public String getPublishDetail() throws Exception {
    	JSONObject jsonObject = new JSONObject();
    	 jsonObject = JsonUtil.put(jsonObject, "publishDetail", service.getPublishDetail(getRequest("id", "")));//获取公告信息
    	return MingUtil.encrypt(jsonObject.toString());
    }
    
    @RequestMapping("getPublishDetailByParam")
    @ResponseBody
    public String getPublishDetailByParam() throws Exception {
    	JSONObject jsonObject = new JSONObject();
    	jsonObject = JsonUtil.put(jsonObject, "publishDetail", service.getPublishDetailByParam(getRequest("type", ""),getRequest("mname", "")));//获取公告信息
    	return MingUtil.encrypt(jsonObject.toString());
    }

}
